import java.util.Deque;
import java.util.PriorityQueue;
import java.util.LinkedList;
import java.util.List;
import java.util.ArrayList;
import java.util.Queue;

public class TextBinaryTree {
    static class TreeNode {
        char val;//数据域
        TreeNode left;//左孩子的引用
        TreeNode right;//右孩子的引用

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //public TreeNode root;//一颗树的根节点
    public TreeNode creatTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');
        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        C.left = F;
        C.right = G;
        E.right = H;
        //this.root=A;
        return A;
    }

    //先序遍历   根节点 左子树 右子树
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    //后序遍历
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    // 获取树中节点的个数
    //public int count=0;
    int size(TreeNode root) {
        if (root == null) {
            return 0;
        }
        // count++;
        int leftCount = size(root.left);
        int rightCount = size(root.right);
        //return count;
        return leftCount + rightCount + 1;
    }

    // 获取叶子节点的个数
    int getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount(root.left) + getLeafNodeCount(root.right);
    }

    // 子问题思路-求叶子结点个数
    // 获取第K层节点的个数
    int getKLevelNodeCount(TreeNode root, int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        int leftSize = getKLevelNodeCount(root.left, k - 1);
        int rightSize = getKLevelNodeCount(root.right, k - 1);
        return leftSize + rightSize;
    }

    // 获取二叉树的高度
    int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        int leftSize = getHeight(root.left);
        int rightSize = getHeight(root.right);
        int max = Math.max(leftSize, rightSize);
        return max + 1;

    }

    // 检测值为value的元素是否存在
    TreeNode find(TreeNode root, int val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        TreeNode leftFind = find(root.left, val);
        if (leftFind != null) {
            return leftFind;
        }
        TreeNode rightFind = find(root.right, val);
        if (rightFind != null) {
            return rightFind;
        }

        return null;
    }

    //层序遍历
   public void levelOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }

    //分层遍历
   public  List<List<Character>> levelOrder1(TreeNode root) {
       List<List<Character>> list=new ArrayList<>();
        if (root == null) {
            return list;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Character> tmp=new ArrayList<>();
            while (size > 0) {
                TreeNode cur = queue.poll();
                //System.out.print(cur.val + " ");
                tmp.add(cur.val);
                size--;

                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }

            }
            list.add(tmp);
        }
        return list;
    }
    // 判断一棵树是不是完全二叉树
    /*boolean isCompleteTree(Node root){

    }
    */
    public int[] maxK(int[] arr, int k) {
        int[] ret = new int[k];
        if(arr == null || k == 0) {
            return ret;
        }

        Queue<Integer> minHeap = new PriorityQueue<>(k);

        for (int i = 0; i <k ; i++) {
            minHeap.offer(arr[i]);
        }

        for (int i = k; i < arr.length; i++) {
            int peek= minHeap.peek();
            if(peek<arr[i]){
                minHeap.poll();
                minHeap.offer(arr[i]);
            }
        }

        for (int i = 0; i < k; i++) {
              ret[i]= minHeap.poll();
        }
         return ret;
    }
}

